package com.ruoyi.web.admin.controller.system;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.qo.UserListQo;
import com.ruoyi.system.domain.vo.AppUserVO;
import com.ruoyi.system.domain.vo.UserAdminListVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IAppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 用户信息操作处理
 *
 * @author ruoyi
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/system/userinfo")
public class AppUserController extends BaseController {

    private final IAppUserService appUserService;

    public AppUserController(IAppUserService appUserService) {
        this.appUserService = appUserService;
    }

    /**
     * 获取用户列表
     */
    @ApiOperation(value = "获取用户列表", notes = "获取所有用户信息")
    @PreAuthorize("@ss.hasPermi('system:userinfo:list')")
    @GetMapping("/appUserList")
    public TableDataInfo appUserList(AppUser appUser) {
        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 获取用户列表
     */
    @ApiOperation(value = "获取用户列表", notes = "获取所有用户信息")
    @PreAuthorize("@ss.hasPermi('system:userinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserListQo userListQo) {
        startPage();
        List<AppUserVO> list = appUserService.selectUserList(userListQo);
        return getDataTable(list);
    }

    /**
     * 获取用户列表
     */
    @ApiOperation(value = "获取用户分页列表", notes = "获取用户分页列表")
    @PreAuthorize("@ss.hasPermi('system:userinfo:list')")
    @GetMapping("/page")
    public TableDataInfo page(UserListQo userListQo) {
        startPage();
        List<UserAdminListVO> list = appUserService.selectUserAdminList(userListQo);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @ApiOperation(value = "获取用户详细信息", notes = "根据id获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:userinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(appUserService.selectAppUserById(id));
    }

    /**
     * 新增用户
     */
    @ApiOperation(value = "新增用户", notes = "新增用户信息")
    @PreAuthorize("@ss.hasPermi('system:userinfo:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody AppUser appUser) {
        return toAjax(appUserService.insertAppUser(appUser));
    }

    /**
     * 修改用户
     */
    @ApiOperation(value = "修改用户", notes = "修改用户信息")
    @PreAuthorize("@ss.hasPermi('system:userinfo:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody AppUser appUser) {
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 删除用户
     */
    @ApiOperation(value = "批量删除用户", notes = "批量删除用户信息")
    @ApiImplicitParam(name = "ids", value = "用户ID数组", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('system:userinfo:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(appUserService.deleteAppUserByIds(ids));
    }

    /**
     * 删除单个用户
     */
    @ApiOperation(value = "删除单个用户", notes = "删除单个用户信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:userinfo:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return toAjax(appUserService.deleteAppUserById(id));
    }


    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @ApiOperation("修改用户密码")
    @PostMapping("/updatePwd")
    public AjaxResult updatePwd(@RequestBody Map<String, String> params) {
        String newPassword = params.get("newPassword");
        Long userId = Long.valueOf(params.get("userId"));

        AppUser appUser = appUserService.selectAppUserById(userId);
        if (Objects.isNull(appUser)) {
            throw new RuntimeException("用户不存在");
        }

        newPassword = SecurityUtils.encryptPassword(newPassword);
        if (appUserService.updatePwd(userId, newPassword) > 0) {
            // 更新缓存用户密码
            return success();
        }
        return error("修改密码异常，请联系管理员");
    }
}